
function brandOrAttributeClick(){
	if(!filterPriceInput){
	  return false;
	}
	var search_json = '';
	search_json = getPost4FreeSearchMessage(
					getPromotionSearchMessage(
						getPriceSearchMessage(
							getOrderSearchMessage(
								getcrumbMessage(search_json)))));
	if(search_json){
	  search_json += ("&"+$(this).attr("value"));
	}else{
	  search_json = $(this).attr("value");
	}
	top.window.location.href = top.window.location.pathname+'?'+search_json;
}

/**
 * 1 判断点击的是否与当前排序是否一样 
 * 2 一样的话 判断是不是综合排序 若是 不做任何处理  若不是 反向排序
 * 3 不一样的话  所有的默认降序
 */
function orderClick(){
	if(!filterPriceInput){
	  return false;
	}
	var search_json = '';
	search_json = getPost4FreeSearchMessage(
					getPromotionSearchMessage(
						getPriceSearchMessage(
							getcrumbMessage(search_json))));
	if($(this).hasClass("on")){
	  if($(this).children('a').children().length){
	  	if(search_json){
	  	  search_json += ("&order="+$(this).children('a').attr("value"));
	  	}else{
	  	  search_json += ("order="+$(this).children('a').attr("value"));
	  	}
	  	if($(this).children('a').children('em').attr("value") == 'DESC'){
	  	  search_json += "&direction=ASC";
	  	}else{
	  	  search_json += "&direction=DESC";
	  	}
	  }else{//重复点击综合排序不做任何处理
	    return false;
	  }
	}else{
		if($(this).children('a').children().length){
		  if(search_json){
		  	 search_json += ("&order="+$(this).children('a').attr("value"));
		  }else{
		  	 search_json += ("order="+$(this).children('a').attr("value"));
		  }
		}else{
		  if(search_json){
		  	 search_json += "&order=COMMON";
		  }else{
		  	 search_json += "&order=COMMON";
		  }
		}
	  	search_json += "&direction=DESC";//默认降序
	}
	top.window.location.href = top.window.location.pathname+'?'+search_json;
}

function promotionOrPost4FreeClick(){
  if(!filterPriceInput){
	  return false;
	}
  var search_json = '';
  search_json = getPost4FreeSearchMessage(
  					getPromotionSearchMessage(
  						getPriceSearchMessage(
  							getOrderSearchMessage(
  								getcrumbMessage(search_json)))));
  top.window.location.href = top.window.location.pathname+'?'+search_json;
}

function cancelPriceSearch(){
	$(".priceRange input.min").val('');
	$(".priceRange input.max").val('');
	priceSearch();
}

function submitPriceSearch(){
  if(!filterPriceInput){
	  return false;
	}
  priceSearch();
}

function priceSearch(){
  var search_json = '';
  search_json = getPost4FreeSearchMessage(
  					getPromotionSearchMessage(
  						getPriceSearchMessage(
  							getOrderSearchMessage(
  								getcrumbMessage(search_json)))));
  console.info(search_json);  								
  top.window.location.href = top.window.location.pathname+'?'+search_json;
}

function getcrumbMessage(search_json){
	if($(".hd_crumb a.brand")[0]){
		if(search_json){
		  search_json +=("&brand="+$(".hd_crumb a.brand").attr("value"));
		}else{
		  search_json +=("brand="+$(".hd_crumb a.brand").attr("value"));
		}
	}
	$(".hd_crumb a.attribute").each(function(){
		if(search_json){
		  search_json +=("&goodAttributes="+$(this).attr("value"));
		}else{
		  search_json +=("goodAttributes="+$(this).attr("value"));
		}
	});
	return search_json;
}

function getOrderSearchMessage(search_json){
	if($(".sort_set li.on a").children().length){//不是综合排序
		if(search_json){
		  search_json +=("&order="+$(".sort_set li.on a").attr("value"));
		  search_json +=("&direction="+$(".sort_set li.on a em").attr("value"));
		}else{
		  search_json +=("order="+$(".sort_set li.on a").attr("value"));
		  search_json +=("&direction="+$(".sort_set li.on a em").attr("value"));
		}
	}else{
	  if(search_json){
	    search_json +="&order=COMMON";
	  }else{
	    search_json +="order=COMMON";
	  }
	}
	return search_json;
}

function getPriceSearchMessage(search_json){
  if($(".priceRange input.min").val()){
	  if(search_json){
	    search_json += ("&minprice="+$(".priceRange input.min").val());
	  }else{
	    search_json += ("minprice="+$(".priceRange input.min").val());
	  }
	}
	if($(".priceRange input.max").val()){
	  if(search_json){
	    search_json += ("&maxprice="+$(".priceRange input.max").val());
	  }else{
	    search_json += ("maxprice="+$(".priceRange input.max").val());
	  }
	}
	return search_json;
}

function getPromotionSearchMessage(search_json){
	if($(".srch_ckb input[name='discount']").next().hasClass("checked")){
	  if(search_json){
	    search_json += "&isPromotion=true";
	  }else{
	    search_json += "isPromotion=true";
	  }
	}
	return search_json;
}

function getPost4FreeSearchMessage(search_json){
	if($(".srch_ckb input[name='ship']").next().hasClass("checked")){
	  if(search_json){
	    search_json += "&isPost4Free=true";
	  }else{
	    search_json += "isPost4Free=true";
	  }
	}
	return search_json;
}
function getOtherSearchMessage(search_json){
	return search_json;
}

//检查价格输入框输入的值是否合法
function filterPriceInput(){
	if(new RegExp('[^\\d]','g').test($(".priceRange input.min").val())){
		return false;
	}
	if($(".priceRange input.min").val().split('.').length>1){
		return false;
	}
	if(new RegExp('[^\\d]','g').test($(".priceRange input.max").val())){
		return false;
	}
	if($(".priceRange input.max").val().split('.').length>1){
		return false;
	}
	return true;
}

$(function(){
  /*文本框事件*/
  var obj = $(".input_notice .txt");
  search_txt(obj);

  /*筛选出前三行的其余隐藏*/
  var obj = $("#catgry_pg .filter .prop_attr .attr");
  attr_hid(obj,3);

  /*更多选项 精简事件*/
  more_less("#catgry_pg");

  /*价格段 重写文本事件*/
  $("#catgry_pg .sort_bar .priceRange .pr_box .input_notice .txt").bind({
    'focus':function(){
       $("#catgry_pg .sort_bar .priceRange").addClass("on");
     },
    'blur':function(){
      setTimeout('$("#catgry_pg .sort_bar .priceRange").removeClass("on")',200);
      //$("#catgry_pg .sort_bar .priceRange").removeClass("on");
    }
  });
  
  /*复选框事件*/
  srch_ctgry_ckb("#catgry_pg");

  /*左侧菜单事件*/
  $("#catgry_pg .sliver .cate>dl dt").click(function(){
    if($(this).parent().hasClass("on")){
      $(this).parent().removeClass("on");
      $(this).parent().find("dd").removeClass("on");
    }else{
      $(this).parent().addClass("on").siblings().removeClass("on");
    }
  });
  $("#catgry_pg .sliver .cate>dl dd>h3 i").click(function(){
    if($(this).parent().parent().hasClass("on")){
      $(this).parent().parent().removeClass("on");
    }else{
      $(this).parent().parent().addClass("on").siblings().removeClass("on");
    }
  });

  /*切换商品图*/
  $(".ctgry_box .imgs .item").hover(function(){
    var newSrc = $(this).find("img").attr("thumb");
    $(this).parents(".ctgry_box").find("a img").attr("src",newSrc);
  });

  /*全部分类事件*/
  $("#header_md .nav_main .bar .all").hover(function(){
    $(this).addClass("show_cate");
  },function(){
    $(this).removeClass("show_cate");
  });
  
  $(".filter div:first a").bind("click",brandOrAttributeClick);
  $(".prop_attr a").bind("click",brandOrAttributeClick);
  $(".sort_bar .sort_set li").bind("click",orderClick);
  $(".srch_ckb span.ckb").bind("click",promotionOrPost4FreeClick);
  $(".priceRange .pr_btn button.btn-clear").bind("click",cancelPriceSearch);
  $(".priceRange .pr_btn button.btn-yes").bind("click",submitPriceSearch);
});